#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <strings.h>

int main(int argc ,char *argv[])
{   
    if (argc != 2)
    {
        printf(" Usage:%s <filename>\n",argv[0]);
        return -1;
    }
    
    //创建匿名管道
    int fd[2];
    pipe(fd);
    //子进程
    if (fork() == 0)
    {
        close(fd[0]); //用写端，关掉读端

        dup2(fd[1], STDOUT_FILENO); //复制fd【1】描述符尾指定的STDOUT_FILED
        
        execlp("ls","ls",argv[1],NULL);
    }
    else //父进程
    {
        close(fd[1]);

        dup2(fd[0], STDIN_FILENO);//复制fd[0]到指定的STDIN_FILEND

        execlp("wc","wc","-w",NULL);
    }
    return 0;
}